<template>
  <div class="bottom-bar">
    <div class="all-checked">
      <check-button class="check-button"
                    :is-checked="isSelectAll"
                    @click.native="checkClick">
      </check-button>
      <span>全选</span>
    </div>
    <div class="price" :key="totalPrice">合计：{{totalPrice}}</div>
    <div class="calculate">
      结算
      <span>({{checkLength}}})</span>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";

import CheckButton from "@/components/content/checkButton/CheckButton";
export default {
  name: "CartButtomBar",
  components: {
    CheckButton,
  },
  computed: {
    ...mapGetters(["cartList"]),
    totalPrice() {
      return (
        "￥" +
        this.cartList
          .filter((item) => {
            return item.checked;
          })
          .reduce((preValue, item) => {
            return preValue + item.price * item.count;
          }, 0)
          .toFixed(2)
      );
    },

    checkLength() {
      return this.cartList.filter((item) => {
        return item.checked;
      }).length;
    },

    isSelectAll() {
      return (
        this.cartList.length !== 0 &&
        this.cartList.filter((item) => item.checked).length ===
          this.cartList.length
      );
    },
  },
  methods: {
    checkClick() {
      if (this.isSelectAll) { // 全部不选中
        this.cartList.forEach(item => item.checked = false);
      } else { // 全部选中
        this.cartList.forEach(item => item.checked = true);
      }
    }
  }
};
</script>

<style scoped>
.bottom-bar {
  display: flex;
  align-items: center;
  height: 44px;
  line-height: 44px;
  border-top: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  background-color: #fff;
}

.all-checked {
  display: flex;
  align-items: center;
  margin-left: 10px;
  width: 60px;
}

.check-button {
  display: flex;
  align-items: center;
  position: relative;
  left: -3px;
  flex-shrink: 0;
  width: 20px;
  margin-right: 3px;
}

.all-checked span {
  display: inline-block;
  font-size: 14px;
  color: #aaa;
}

.price {
  flex: 1;
  font-size: 16px;
}

.calculate {
  width: 90px;
  background: #ff8198;
  text-align: center;
  font-size: 16px;
  color: #fff;
  /* border-radius: 20px; */
}

.calculate span {
  font-size: 14px;
}
</style>
